<template>
  <div>
    <div>
      <c-table-box
        ref="cTableBox"
        hide-func
        checkbox
        :other-filters="otherFilters"
        :table-config="tableConfig"
        @selection-change="selectionChange"
      >
        <template #btn>
          <c-permission v-if="type==='all'" code="all">
            <c-button opt-code="add" type="primary" @click="handleAdd">新增</c-button>
            <EditPerson :sel-data="tableSelected" opt-code="editPerson" @on-success="refreshTable" />
            <!-- <c-button opt-code="export" type="primary" @click="handleExport">导出</c-button> -->
          </c-permission>
          <c-permission v-if="type==='awaitSubmit'" code="awaitSubmit">
            <c-button opt-code="edit" type="primary" @click="handleEdit">编辑</c-button>
            <c-button opt-code="delete" type="primary" @click="handleDelete">删除</c-button>
          </c-permission>
          <c-permission v-if="type==='problemAudit'" code="problemAudit">
            <c-button opt-code="auditApply" type="primary" @click="handleAuditApply('applyAudit')">申请审核</c-button>
          </c-permission>
          <c-permission v-if="type==='awaitRectify'" code="awaitRectify">
            <c-button opt-code="uploadRectifyFile" type="primary" @click="handleAuditApply('2')">上传整改资料</c-button>
          </c-permission>
          <c-permission v-if="type==='rectifyAduit'" code="rectifyAduit">
            <c-button opt-code="rectifyAduit" type="primary" @click="handleAuditApply(1)">审核</c-button>
          </c-permission>
          <c-permission v-if="type==='rectifyReaduit'" code="rectifyReaduit">
            <c-button opt-code="rectifyReaduit" type="primary" @click="handleAuditApply(1)">审核</c-button>
          </c-permission>
        </template>
      </c-table-box>
      <!-- <Edit v-if="showEdit" ref="refEditDlg" :detail-data="activeRowData" :visible.sync="showEdit" @success="refreshTable" /> -->
      <Audit
        v-if="showAudit"
        ref="refAuditDlg"
        :patrol-id="activeRowData.id"
        :visible.sync="showAudit"
        @success="refreshTable"
      />
      <export-files v-if="exportVisible" title="导出" :config="exportAllConfig" :visible.sync="exportVisible" @success="handleExportSuccess" />
    </div>
    <!-- <Detail v-if="showDetail" ref="refEditDlg" :detail-data="activeRowData" :visible.sync="showDetail" /> -->
  </div>
</template>
<script>
import { otherFiltersItem } from '@/utils'
import EditPerson from './components/EditPerson.vue'
import Audit from './components/AuditDlg.vue'
import { deleteTableBatch } from '@/api/marketService/decorateInspection'

export default {
  name: 'List',
  components: { EditPerson, Audit },

  props: {
    type: {
      type: String,
      default: ''
    },
    activeType: {
      type: String,
      default: ''
    },
    serachFilters: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      showDetail: false,
      exportVisible: false,
      typeFilters: [],
      id: '',
      delayLoading: false,
      stopLoading: false,
      doorHeadOptions: [],
      decorationFormOptions: [],
      applyBizTypeOptions: [],
      applySpecialZoneOptions: [],
      activeDataId: '',
      centerDialogVisible: false,
      loading: false,
      confirmVisible: false,
      showEdit: false,
      showAudit: false,
      // showInformation: false,
      activeTabIndex: 0,
      activeName: 'all',
      activeRowData: {},
      showOpenExe: false, // 打开开业执行弹窗
      showResultAudit: false, // 打开开业结果审核弹窗
      handlefilters: [],
      showApply: false,
      tableSelected: [], // 当前所选中表格行数据
      tableConfig: {
        interfacePath: `${process.env.VUE_APP_BASE_API}/api/v1/fitPatrol/page`,
        buttonGroupCode: 'default',
        hideSearchBar: true,
        tabIndex: 0,
        pageSize: 10,
        order: 'desc',
        apiHeader: {
          'Permission-perCode': 'decorate_inspection',
          'Permission-App': 'slm'
        },
        // sort: 'createTime',
        // 表格字段动态配置
        setting: [
          {
            prop: 'patrolTaskNo',
            label: '装修巡检任务号',
            minWidth: '110',
            className: 'btn-green',
            isButton: true,
            clickFun: this.handleDetail
          },
          {
            prop: 'constCode',
            label: '门店建设任务号',
            minWidth: '110'
          },
          {
            prop: 'decorateTaskNo',
            label: '装修任务号',
            minWidth: '130'
          },
          {
            prop: 'schedule',
            label: '巡检任务进度',
            minWidth: '110',
            propEnum: {
              domainCode: 'marketing',
              appCode: 'slm',
              dictTypeCode: 'PATROL_INSPECTION'
            }
          },
          {
            prop: 'customerCode',
            label: '客户编码',
            minWidth: '110'
          },
          {
            prop: 'customerName',
            label: '客户名称',
            minWidth: '110'
          },
          {
            prop: 'shopCode',
            label: '门店编码',
            minWidth: '110'
          },
          {
            prop: 'shopName',
            label: '门店名称',
            minWidth: '110'
          },
          {
            prop: 'operateArea',
            label: '门店档案面积',
            minWidth: '110'
          },
          {
            prop: 'proSupervisorName',
            label: '工程监理',
            minWidth: '110'
          },
          // {
          //   prop: 'officeLinkMan',
          //   label: '办事处主管',
          //   minWidth: '110'
          // },
          // {
          //   prop: 'finishedManagerName',
          //   label: '成品客户经理',
          //   minWidth: '110'
          // },
          // {
          //   prop: 'customManagerName',
          //   label: '定制客户经理',
          //   minWidth: '110'
          // },
          {
            prop: 'personName',
            label: '巡检人员',
            minWidth: '110'
          }
        ]
      }
    }
  },
  computed: {
    otherFilters() {
      return [...this.serachFilters, ...this.typeFilters]
    },
    selectedIds() {
      return this.tableSelected.map(row => {
        return row.id
      })
    },
    exportAllConfig() {
      return [{
        code: 'export',
        name: '洽谈需求',
        dataApi: '/slm-admin/api/v1/decorationDelegateDetail/exportData',
        selectedRows: [], // this.tableSelected,
        paramsRaw: {},
        headers: { 'Permission-perCode': '' },
        filtersRaw: this.otherFilters,
        queryField: 'decorationDelegateDetailId'
      }]
    }
  },
  watch: {
    type: {
      handler(v) {
        this.setOtherFilters(v)
      },
      immediate: true
    },
    serachFilters: {
      handler(v) {
        if (this.activeType !== this.type) return
        this.refreshTable('')
      }
    }
  },
  activated() {
    if (this.ready && this.refreshTable) {
      this.refreshTable()
    }
  },
  deactivated() {
    this.ready = true
  },
  created() {
  },
  methods: {

    selectionChange(v) {
      this.tableSelected = v
    },

    // 导出
    handleExport() {
      this.exportVisible = true
    },
    handleDetail(val) {
      this.$router.push({ name: 'DecorateInspectionEdit', query: { id: val.id, isDetail: 1 }})
    },

    // 设置过滤条件
    setOtherFilters(v) {
      switch (v) {
        case 'all':
          this.typeFilters = []
          break
        case 'awaitSubmit':
          this.typeFilters = [otherFiltersItem('schedule', 'A', '=')]
          break
        case 'problemAudit':
          this.typeFilters = [otherFiltersItem('schedule', 'B', '=')]
          break
        case 'awaitRectify':
          this.typeFilters = [otherFiltersItem('schedule', 'C', '=')]
          break
        case 'rectifyAduit':
          this.typeFilters = [otherFiltersItem('schedule', 'D', '=')]
          break
        case 'rectifyReaduit':
          this.typeFilters = [otherFiltersItem('schedule', 'E', '=')]
          break
      }
    },
    handleAdd() {
      this.$router.push({ name: 'DecorateInspectionEdit' })
    },
    handleAuditApply(type) {
      if (!this.singleJudge(this.tableSelected, '操作', true)) {
        return
      }
      this.$router.push({ name: 'AuditApply', query: { id: this.tableSelected[0].id, type }})
    },
    // 审核资料初审/复审
    handleAduit() {
      if (!this.singleJudge(this.tableSelected, '审核', true)) {
        return
      }
      this.activeRowData = this.tableSelected[0]
      this.showAudit = true
    },
    handleDelete() {
      if (!this.singleJudge(this.tableSelected, '删除', false)) {
        return
      }
      this.$confirm(`是否删除？`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteTableBatch(this.selectedIds).then(res => {
          if (res.success) {
            this.refreshTable(false)
          }
        })
      })
    },
    // 编辑进度
    handleEdit(type) {
      if (!this.singleJudge(this.tableSelected, '编辑', true)) {
        return
      }
      this.$router.push({ name: 'DecorateInspectionEdit', query: { id: this.tableSelected[0].id }})
    },
    // 刷新表格
    refreshTable(v = 'onPage') {
      this.$nextTick(() => {
        this.$refs.cTableBox.initTableData(v)
      })
    },

    // 导出成功
    handleExportSuccess() {
      this.$message.success('导出成功！')
    }
  }
}
</script>
